package com.rt.easyjava.base.biz1.controller.app;

import com.rt.easyjava.base.biz1.bean.TmptBaseFileBiz;
import com.rt.easyjava.base.biz1.service.intf.TmptBaseFileBizService;
import com.rt.easyjava.base.utils.DfsService;
import com.rt.easyjava.base.utils.ImgUtil;
import com.rt.easyjava.common.base.Result;
import com.rt.easyjava.common.exception.ControllerException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: jxl
 * @Date: 2020/06/18
 * @Time: 15:22
 */
@RestController
@RequestMapping("/fdfs")
@Api(value = "/fdfs", tags = "FastDfsUploadController -  IMG上传")
public class FastDfsUploadController extends ControllerException {

    @Resource
    protected TmptBaseFileBizService tmptBaseFileBizService;
    @Resource
    private DfsService dfsService;

    @Value("${fdfs.uri}")
    private String urlPrefix;

    @Value("${fdfs.tokens}")
    private String tokens;

    // @PreAuthorize("hasAuthority('image:upload')")
    @PostMapping(value = "/uploadImgBase64")
    @ApiOperation("Base64图片上传")
    public Result uploadImgBase64(@RequestBody Map<String, String> paramMap, String userDetailsJson) throws Exception {
        TmptBaseFileBiz tmptBaseFileBiz = new TmptBaseFileBiz();
        tmptBaseFileBiz.setUserDetailsJson(userDetailsJson);
        tmptBaseFileBiz.setTmptBaseFileBizService(tmptBaseFileBizService);
        String fileBase64 = paramMap.get("fileBase64");
        String token = paramMap.get("token");
        if (!checkToken(token).isSuccess()) {
            return Result.builder().fault("token error");
        }
        MultipartFile file = ImgUtil.base64ToMultipart(fileBase64);
        Map<String, Object> map = dfsService.uploadFile(file, tmptBaseFileBiz);
        if (map.get("code").equals("0000")) {
            Map resultMap = new HashMap();
            String filePath = map.get("fileId").toString();
            String downloadUrl = urlPrefix + "/" + filePath;
            resultMap.put("group", filePath);
            resultMap.put("url", downloadUrl);
            return Result.builder(resultMap);
        }
        return Result.builder();
    }

    // 未测试，可能传参有问题
    // @PreAuthorize("hasAuthority('image:upload')")
    @PostMapping(value = "/uploadImgInputStream")
    @ApiOperation("InputStream图片上传")
    public Result uploadImgInputStream(@RequestBody InputStream inputStream, String userDetailsJson) throws Exception {
        TmptBaseFileBiz tmptBaseFileBiz = new TmptBaseFileBiz();
        tmptBaseFileBiz.setUserDetailsJson(userDetailsJson);
        tmptBaseFileBiz.setTmptBaseFileBizService(tmptBaseFileBizService);
        Map<String, Object> map = dfsService.uploadFile(inputStream, tmptBaseFileBiz, "jpg");
        if (map.get("code").equals("0000")) {
            Map resultMap = new HashMap();
            String filePath = map.get("fileId").toString();
            String downloadUrl = urlPrefix + "/" + filePath;
            resultMap.put("group", filePath);
            resultMap.put("url", downloadUrl);
            return Result.builder(resultMap);
        }
        return Result.builder();
    }

    // 未测试，可能传参有问题
    // @PreAuthorize("hasAuthority('image:upload')")
    @PostMapping(value = "/uploadImg", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, headers = "content-type=" + MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiOperation("图片上传")
    public Result uploadImg(MultipartFile file, String userDetailsJson) throws Exception {
        TmptBaseFileBiz tmptBaseFileBiz = new TmptBaseFileBiz();
        tmptBaseFileBiz.setUserDetailsJson(userDetailsJson);
        tmptBaseFileBiz.setTmptBaseFileBizService(tmptBaseFileBizService);
        Map<String, Object> map = dfsService.uploadFile(file, tmptBaseFileBiz);
        if (map.get("code").equals("0000")) {
            Map resultMap = new HashMap();
            String filePath = map.get("fileId").toString();
            String downloadUrl = urlPrefix + "/" + filePath;
            resultMap.put("group", filePath);
            resultMap.put("url", downloadUrl);
            return Result.builder(resultMap);
        }
        return Result.builder();
    }

    private Result checkToken(String token) {
        List tokenList = Arrays.asList(tokens.split(";"));
        if (tokenList.contains(token)) {
            return Result.builder();
        }
        return Result.builder().fault("token error");
    }


}
